<?php
/**
 * Created by PhpStorm.
 * User: wangwenfan
 * Date: 2018/8/3
 * Time: 16:45.
 */

namespace App\Admin\Controllers;

use App\Models\Base\ServerList;
use App\Models\Game\WebOpenActTime;
use App\Service\RequestService;
use App\Service\ServerService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class ActivityController extends Controller
{
    use ModelForm;

    /**
     * Create interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.gm_tool'));
            $content->description(trans('game.activity_config'));
            $content->body($this->grid());
            $this->activeScript();
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(WebOpenActTime::class, function (Grid $grid) {
            $grid->activity_id(trans('activity_id'));
            $server_id = $this->server_id;
            $grid->column(trans('game.server_name'))->display(function () use ($server_id) {
                return ServerService::getServerName($server_id);
            });
            $grid->column(trans('game.activity_name'))->display(function () {
                return getActivityName($this->activity_id);
            });
            $grid->timeOpen(trans('game.start_time'))->display(function ($time_open) {
                return date('Y-m-d', $time_open);
            });
            $grid->timeEnd(trans('game.end_time'))->display(function ($time_end) {
                return date('Y-m-d', $time_end);
            });
            $grid->disableExport();
            $grid->actions(function ($actions) use ($grid,$server_id) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->append('<a href="'.route('activity.edit', ['id' => $this->row->id]).
                    '?server_id='.$server_id.'" style="margin-right: 5px" "><i class="fa fa-edit"></i></a>');
            });
            $grid->tools(function ($tools) {
                $button = '<a class="btn btn-sm btn-success pull-right"  id="activate"
                            style="margin-right: 10px;">'.trans('game.activate_activity').'</a>';
                $tools->append($button);
                $tools->append(view('admin.tools.activity'));
            });
            // 字段过滤器
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->setNotWhere(['server_id']);
                $filter->equal('server_id', trans('game.server_name'))->select(ServerList::all()->pluck('name', 'server_id'));
            });
        });
    }

    /**
     * 激活活动js.
     */
    protected function activeScript()
    {
        $string = <<<EOF
            $("#activate").on('click',function(){
                $("#activity-modal").modal();
            });
            $(".modal-backdrop").remove();
EOF;
        Admin::script($string);
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.gm_tool'));
            $content->description(trans('admin.new'));
            $content->body($this->form());
        });
    }

    protected function form()
    {
        return Admin::form(WebOpenActTime::class, function (Form $form) {
            $form->display('id', 'ID');
            $method = request()->method();
            if (request()->get('server_id') || 'PUT' == $method) {
                $form->text('server',trans('game.server_name') )->default($this->server_id)->readOnly();
                $form->hidden('server_id')->default($this->server_id);
                $form->radio('switch', trans('game.activity_close'))->options([1 => '是', 0 => '否'])->default(1);
            } else {
                $form->select('server_id', trans('game.server_name'))
                    ->options(ServerList::all()->pluck('name', 'server_id'))
                    ->default($this->server_id)
                    ->rules('required|numeric');
                $form->dateRange('timeOpen', 'timeEnd', trans('game.activity_time'));
            }
            $form->checkbox('activity_id', trans('game.select_activity'))->options(config('game.activity'))
                ->rules('required')->stacked();

            $form->saving(function (Form $form) use ($method) {
                //发送给游戏
                $input = request()->all();
                foreach ($input['activity_id'] as $a_id) {
                    if (!$a_id) {
                        continue;
                    }
                    if ('POST' == $method) {
                        $option = '17;'.$a_id.';'.strtotime($input['timeOpen']).';'.strtotime($input['timeEnd']);
                    } else {
                        if (1 == $input['switch']) {
                            $option = '18;0;'.$a_id;
                        } else {
                            $option = '17;'.$a_id.';'.strtotime($input['timeOpen']).';'.strtotime($input['timeEnd']);
                        }
                    }
                    $ret = RequestService::sendVip($option, $input['server_id']);
                }
                if (!$ret) {
                    return back()->with(admin_toastr(trans('game.gs_communication_failed'), 'error'))->withInput();
                } else {
                    admin_toastr(trans('admin.succeeded'));

                    return redirect(route('activity.index'));
                }
            });
        });
    }

    /**
     * 激活活动.
     */
    public function activation(Request $request)
    {
        if (false == $request->get('server_id')) {
            return back()->with(admin_toastr(trans('game.select_server'), 'error'));
        }
        $option = '18;1;0';
        $ret = RequestService::sendVip($option, $this->server_id);
        if (!$ret) {
            return back()->with(admin_toastr(trans('game.gs_communication_failed'), 'error'));
        } else {
            return back()->with(admin_toastr(trans('admin.succeeded')));
        }
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('game.activity_config'));
            $content->description(trans('admin.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * 关闭活动.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if ($this->form()->destroy($id)) {
            return response()->json([
                'status' => true,
                'message' => trans('admin.delete_succeeded'),
            ]);
        } else {
            return response()->json([
                'status' => false,
                'message' => trans('admin.delete_failed'),
            ]);
        }
    }
}
